home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C013.ZIP / HDR.C < prev    next >
Text File  |  1990-01-19  |  3KB  |  113 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.013        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: hdr.c
  7.  * Program name:hdr 
  8.  * Source of file: Ron Wellstead
  9.  * Purpose: An MS-DOS copy of the UNIX utility of the same name.
  10.  * Changes: <who what when & why major changes have been made>      
  11.  ********************************************************************/
  12.  
  13. /*
  14.  *
  15.  *    @(#) hdr.c 1.2 87/07/27 
  16.  *
  17.  *    UNIX style hdr utility for dos
  18.  *
  19.  *    copyright (c) 1987 Ron Wellsted.
  20.  *    This software is provided on the understanding that it is
  21.  *    NOT to be used for commercial gain. It may be freely distributed
  22.  *    in source or object form among amateur and hobby computer users ONLY!
  23.  *
  24.  *    displays the .exe header of files on stdout
  25.  *    usage:    hdr files....
  26.  *    written for Microsoft C, link with setargv.obj to expand wildcards
  27.  */
  28. #include    <fcntl.h>
  29. #include    <sys/types.h>
  30. #include    <sys/stat.h>
  31. #include    <io.h>
  32. #include    <stdio.h>
  33.  
  34. char what[] = "@(#)hdr.c VR 1.0.0 15 Jul 1987";
  35.  
  36. struct
  37. {
  38.     unsigned magic,        /* magic no.(0x5a4d)    */
  39.     bytes,            /* bytes in last page    */
  40.     pages,            /* no. of pages in file    */
  41.     nreloc,            /* no. of reloc entries    */
  42.     hdrsz,            /* size of header (para)*/
  43.     minalloc,        /* min alloc size (para)*/
  44.     maxalloc,        /* max alloc size (para)*/
  45.     initss,            /* initial SS value    */
  46.     initsp,            /* initial SP value    */
  47.     chcksum,        /* -ve checksum of file    */
  48.     initip,            /* initial IP value    */
  49.     initcs,            /* initial CS value    */
  50.     rlocoff,        /* byte offs to reloc    */
  51.     ovrlay;            /* overlay no.        */
  52. } exe_hdr;
  53.  
  54. main (argc,argv)
  55. int argc;
  56. char *argv[];
  57. {
  58.     int i,j;
  59.  
  60.     if (argc<2)
  61.     {
  62.         fprintf(stderr,"Usage: hdr file [... fileN]\n");
  63.         exit(1);
  64.     }
  65.     for (i=1;i<argc;i++)
  66.     {
  67.         if ((j=hdr(argv[i]))>0)
  68.             fprintf(stderr,"hdr: %s: not an .exe format file\n",
  69.                 argv[i]);
  70.         else if (j<0)
  71.         {
  72.             fprintf(stderr,"hdr: ");
  73.             perror(argv[i]);
  74.         }
  75.     }
  76.     exit(0);
  77. }
  78.  
  79. hdr(ptr)
  80. char *ptr;
  81. {
  82.     int fh, i;
  83.     unsigned long l;
  84.  
  85.     if (( fh = open(ptr, O_RDONLY|O_BINARY)) < 0) return fh;
  86.     lseek(fh, 0L, 0);
  87.     i = sizeof(exe_hdr);
  88.     if (i != read(fh, (char *)(&exe_hdr), i)) return 1;
  89.     else if (exe_hdr.magic != 0x5a4d) return 1;
  90.     else
  91.     {
  92.         printf("file %s :\n",ptr);
  93.         printf("magic number       =    0x%x\n", exe_hdr.magic);
  94.         l = (long)(exe_hdr.pages - 1) * 512L;
  95.         l += (long)(exe_hdr.bytes);
  96.         printf("file length        =    %6ld\n", l);
  97.         printf("relocation size    =    %6d\n", exe_hdr.nreloc);
  98.         printf("header size        =    %6d\n", exe_hdr.hdrsz * 16);
  99.         l = (long)(exe_hdr.minalloc) * 16L;
  100.         printf("minimum allocation =    %6ld\n", l);
  101.         l = (long)(exe_hdr.maxalloc) * 16L;
  102.         printf("maximum allocation =   %7ld\n", l);
  103.         printf("initial SS:SP      = %04x:%04x\n", exe_hdr.initss,
  104.             exe_hdr.initsp);
  105.         printf("initial CS:IP      = %04x:%04x\n", exe_hdr.initcs,
  106.             exe_hdr.initip);
  107.         printf("checksum           =    0x%04x\n", exe_hdr.chcksum);
  108.         printf("relocation offset  =    %6d\n", exe_hdr.rlocoff);
  109.         printf("overlay number     =    %6d\n\n", exe_hdr.ovrlay);
  110.     }
  111.     return close(fh);
  112. }
  113.